<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 轮播图（大）
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                <el-button
                            type="primary"
                            icon="el-icon-edit"
                            @click="handleEdit()"
                        >添加
                </el-button>
            </div>
            <el-table
                v-if="isReloadData"
                :data="swiperData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column
                        label="序号"
                        type="index"
                        width="80"
                        align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.$index + 1}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="轮播图ID" width="155" align="center"></el-table-column>
                <el-table-column label="首页轮播图" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="api + scope.row.image"
                            :preview-src-list="[api + scope.row.image]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template>
                        <el-tag
                            type="success"
                        >成功</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="添加" :visible.sync="editVisible" width="30%">
            <el-upload
            class="avatar-uploader"
            action="http://60.205.252.88:8080/File_Upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import { upHomeSwiper, getHomeSwiper, delHomeSwiper, delNumHomeSwiper } from '../../api/swiper'
export default {
    name: 'basetable',
    data() {
        return {
            isReloadData: true,
            multipleSelection: [],
            delList: [],
            editVisible: false,
            id: -1,
            imageUrl: '',
            swiperData: [],
            upimage: '',
            api: 'http://60.205.252.88:8080',
            itemKey: ''
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取数据
        getData() {
            getHomeSwiper().then(res => {
            this.swiperData = res.data
            console.log('轮播图数据', this.swiperData)
            })
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].id + ',';
            }
             this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    console.log(typeof str, str)
                    delNumHomeSwiper(str).then(res => {
                        this.$message.error(`删除了${str}`);
                        this.multipleSelection = [];
                        this.getData()
                    })
                })
                .catch(() => {});
        },
        // 添加操作
        handleEdit(index, row) {
            this.editVisible = true;
        },
        // 保存并上传
        saveEdit() {
            upHomeSwiper(this.upimage).then(res => {
                console.log(res)
                this.editVisible = false;
                this.$message.success('添加成功');
                this.getData()
            }).catch(err => console.log(err))
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    console.log(row.id)
                    delHomeSwiper(row.id).then(res => {
                        console.log(res)
                    })
                    this.$message.success('删除成功');
                    this.swiperData.splice(index, 1);
                })
                .catch(() => {});
        },
        handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        console.log(res.data)
        this.upimage = res.data
        },
        beforeAvatarUpload(file) {
            const isLt2M = file.size / 1024 / 1024 < 2;
            console.log(file)
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isLt2M;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
/* 图片选择 */
.el-dialog__body{
    width: 184px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
    text-align: center;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    /* width: 178px; */
    height: 178px;
    display: block;
    margin: auto;
  }
</style>
